// pages/start/start.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 登录
    wx.login({
      success: res => {
        wx.request({
          url: 'https://www.zhiweihome.com/zhixiu/stationing/home/get_openid',
          data: {
            code: res.code,
            appid: 'wx12aa072b256c9761',
            secret: '444f48e2b5591487567e7f8ed57c8c2a',
          },
          success: function (suc) {
            wx.setStorage({
              key: 'openid',
              data: suc.data,
              success:function(){
                that.setData({
                  openid: suc.data
                })
                wx.getSetting({
                  success: function (res) {
                    if (res.authSetting['scope.userInfo']) {
                      wx.getUserInfo({
                        success: function (res) {
                          wx.setStorage({
                            key: 'userInfo',
                            data: res.userInfo,
                            success: function () {
                              app.func.req('login', { openid: suc.data, user_headimg: res.userInfo.avatarUrl, user_nickname: res.userInfo.nickName, user_sex: res.userInfo.gender }, 'POST', function (res) {
                                // console.log(res);
                                if (res.code == 200) {
                                  wx.redirectTo({
                                    url: '../index/index',
                                  })
                                }                    
                              });
                            }
                          })
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        })
      }
    })
    
  }, 

  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      wx.setStorage({
        key: 'userInfo',
        data: e.detail.userInfo,
        success: function () {
          // console.log(that.data.openid);
          app.func.req('login', { openid: that.data.openid, user_headimg: e.detail.userInfo.avatarUrl, user_nickname: e.detail.userInfo.nickName, user_sex: e.detail.userInfo.gender}, 'POST', function (res) {
            if (res.code == 200) {
              wx.redirectTo({
                url: '../index/index',
              })
            }
          });
        }
      }) 
    } else {
      wx.showToast({
        title: '请先授权登录！',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})